package com.lance.answer.gui.answer.gk.answer.lms.util;

import com.lance.answer.util.ImageUtil;
import com.lance.answer.util.image_valid.ValidUtils;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.openqa.selenium.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * <p>识别验证码<p>
 *
 * @author : fengx
 * @since JDK8.0
 * create by fengx on 2021/1/21 14:45
 */
public class AnalysisIdentifyingCode {

    public static void takeScreenshot(WebDriver driver, String filePath) throws IOException {
        // 得到截图
        byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
        FileOutputStream fos = new FileOutputStream(filePath);
        fos.write(screenshot);
        fos.close();
    }

    //开始裁剪的位置和截图的宽和高
    public static BufferedImage createElementImage(WebDriver driver, String filePath, int x, int y, int width, int height)
            throws IOException {

        //BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(takeScreenshot(driver)));
        takeScreenshot(driver, filePath);
        //  System.exit(1);
        BufferedImage originalImage = ImageIO.read(new File(filePath));
        // 进行裁剪
        BufferedImage croppedImage = originalImage.getSubimage(x, y, width, height);
        return croppedImage;
    }

    /**
     * 灰度化
     */
    public static BufferedImage grayImage(BufferedImage srcImage) {
        return copyImage(srcImage, new BufferedImage(srcImage.getWidth(), srcImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY));
    }

    /**
     * 二值化
     */
    public static BufferedImage binaryImage(BufferedImage srcImage) {
        return copyImage(srcImage, new BufferedImage(srcImage.getWidth(), srcImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY));
    }

    public static BufferedImage copyImage(BufferedImage srcImage, BufferedImage destImage) {
        for (int y = 0; y < srcImage.getHeight(); y++) {
            for (int x = 0; x < srcImage.getWidth(); x++) {
                destImage.setRGB(x, y, srcImage.getRGB(x, y));
            }
        }
        return destImage;
    }


    public static String getVerificationCode(WebDriver driver, String path)   {
        File imageFile = new File(path);
        try {
            imageFile.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        WebElement element = driver.findElement(By.id("kaptchaImage"));
        Dimension dimension = element.getSize();
        Point point = element.getLocation();
        try {
            //得到裁剪的图片
            BufferedImage image = createElementImage(driver, path, point.x, point.y,
                    dimension.width, dimension.height);
       //    BufferedImage processedImage = binaryImage(grayImage(image));
            BufferedImage processedImage = binaryImage(image);
            //进行保存
         //   ImageIO.write(processedImage, "png", imageFile);
            ImageIO.write(image, "jpeg", imageFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 调用Tesseract

        String result = null;

        try {
            result = ValidUtils.read(path);
        } catch (TesseractException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.err.println("result1111111111111:"+result);
        //替换大小写及数字
        result = result.replaceAll("[^a-z^A-Z^0-9]", "");
        return result;
    }

    public static void main(String[] args) {
        String path="d:\\test\\test2_1.jpg";
        File imageFile = new File(path);
        try {
            //得到裁剪的图片
            BufferedImage processedImage = binaryImage(grayImage(ImageIO.read(new File("d:\\test\\2.jpg"))));
            //进行保存
            ImageIO.write(processedImage, "png", imageFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 调用Tesseract
        ITesseract instance = new Tesseract();

        String result = null;
        try {
            // ImageIO.scanForPlugins();
            File oldFile = new File(path);
            ImageUtil.cleanImage(oldFile,oldFile.getParent());
            result = instance.doOCR(new File(path));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        System.err.println("result1111111111111:"+result);
    }





}
